package hd.util.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


/**
 * 广播工具
 * <p/>
 * Created by haidy on 14/11/22.
 */
public class BroadcastUtil {

    private static Context mContext;

    public static void init(Context context) {
        mContext = context;
    }

    /**
     * 动态注册广播
     *
     * @param receiver 广播对象
     * @param action   活动
     */
    public static void registerAction(BroadcastReceiver receiver, String action) {
        IntentFilter filter = new IntentFilter();
        filter.addAction(action);
        mContext.registerReceiver(receiver, filter);
    }

    /**
     * 取消注册
     *
     * @param receiver 已经注册的广播对象
     */
    public static void unRegisterReceiver(BroadcastReceiver receiver) {
        try {
            mContext.unregisterReceiver(receiver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendBroadcast(Intent intent) {
        mContext.sendBroadcast(intent);
    }

}